using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using Antix;

namespace Antix.Data.Catalogue.Objects
{
    /// <summary>
    /// Database Column Collection
    /// </summary>
    public class DatabaseColumnCollection : DatabaseObjectCollection
    {
        #region Constants

        #endregion

        #region Constructor

        /// <summary>
        /// Create collection
        /// </summary>
        internal DatabaseColumnCollection(DatabaseTable table)
        {
            Table = table;
        }

        #endregion

        #region Properties

        #region Table

        internal readonly DatabaseTable Table;

        #endregion

        #region Indexers

        /// <summary>
        /// Get item by name, if not found a new one is passed back
        /// </summary>
        new public DatabaseColumn this[string name]
        {
            get { return this[name, false]; }
        }

        /// <summary>
        /// Get item by name, if not found a new one is passed back
        /// </summary>
        public DatabaseColumn this[string name, bool create]
        {
            get
            {
                if (this.Contains(name))
                {
                    return (DatabaseColumn)base[name];
                }
                else if(create) { return this.Add(name); }

                return null;
            }
        }

        #endregion

        #endregion

        #region Methods

        #region Add

        /// <summary>
        /// Add a new column
        /// </summary>
        /// <param name="name">Column name</param>
        /// <returns>New column</returns>
        public DatabaseColumn Add(string name)
        {
            DatabaseColumn newItem = new DatabaseColumn(this.Table);
            newItem.Name = name;

            base.Add(newItem);

            return newItem;
        }

        /// <summary>
        /// Add a new column
        /// </summary>
        /// <param name="name">Column name</param>
        /// <returns>New column</returns>
        public DatabaseColumn Add(
            string name,
            string typeName, int length, bool allowNull,
            string defaultValue)
        {
            DatabaseColumn newItem = this.Add(name);
            newItem.TypeName = typeName;
            newItem.Length = length;
            newItem.AllowNull = allowNull;
            newItem.Default.Value = defaultValue;

            return newItem;
        }

        /// <summary>
        /// Add Existing item to collection
        /// </summary>
        /// <param name="item"></param>
        internal void Add(DatabaseColumn item)
        {
            base.Add(item);
        }

        #endregion

        #endregion
    }
}
